package com.agh.szepczynski.magisterka.service.events;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.agh.szepczynski.magisterka.service.algorithms.IContestAlghoritm;
import com.agh.szepczynski.magisterka.service.beans.dm.Person;

public class Event extends Thread {
	protected static final Log logger = LogFactory.getLog(Event.class);
	private String creator;
	private Date dateCreated;
	private Date dateToBeFired;
	private List<Person> people = new ArrayList<Person>();
	private final IContestAlghoritm contestAlghoritm;
	private int winnersCount=1;
	public Event(IContestAlghoritm contestAlghoritm) {
		this.contestAlghoritm = contestAlghoritm;
	}
	
	
	public List<Person> getWinners() throws Exception {
		return contestAlghoritm.getWinners();

	}
	public AlgorithmState getAlgorithmState() {
		return contestAlghoritm.getAlgorithmState();
	}

	public IContestAlghoritm getContestAlghoritm() {
		return contestAlghoritm;
	}

	public String getCreator() {
		return creator;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public List<Person> getPeople() {
		return people;
	}

	@Override
	public void run() {
		logger.debug("Running event");
		// todo run facebook api to get all likes and people
		try {
			contestAlghoritm.init(people,winnersCount);
		} catch (Exception e1) {
			logger.error("Algorithm init error",e1);
			return ;
		}
		while (!contestAlghoritm.isEnded()) {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			contestAlghoritm.nextStep();
		}
		logger.debug("Event ended");
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}
	public void setPeople(List<Person> people) {
		this.people = people;
	}


	public int getWinnersCount() {
		return winnersCount;
	}


	public void setWinnersCount(int winnersCount) {
		this.winnersCount = winnersCount;
	}


	public Date getDateToBeFired() {
		return dateToBeFired;
	}


	public void setDateToBeFired(Date dateToBeFired) {
		this.dateToBeFired = dateToBeFired;
	}

	
	
}
